﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Drawing;

namespace Business {
    /// <summary>
    /// Class for image manipulation
    /// </summary>
    public class ImageEditor {
        /// <summary>
        /// Resizes the image requested.
        /// </summary>
        /// <param name="maxWidth">Width of the max.</param>
        /// <param name="maxHeight">Height of the max.</param>
        /// <param name="image">The image.</param>
        /// <returns>Bitmap image</returns>
        public Bitmap ResizeThisImage(int maxWidth, int maxHeight, Image image) {
            //try to maintain image aspect ratio
            int height = (image.Height * maxWidth) / image.Width;
            int width = maxWidth;
            if (image.Height > maxHeight) {
                width = (image.Width * maxHeight) / image.Height;
                height = maxHeight;
            }

            //start resizing the image
            Bitmap bitmap = new Bitmap(width, height);
            Graphics graph = Graphics.FromImage(bitmap);
            //try to maintain image quality
            graph.CompositingQuality = CompositingQuality.HighQuality;
            graph.SmoothingMode = SmoothingMode.HighQuality;
            graph.InterpolationMode = InterpolationMode.HighQualityBicubic;

            Rectangle imageRectangle = new Rectangle(0, 0, width, height);
            graph.DrawImage(image, imageRectangle);

            graph.Dispose();

            return bitmap;
        }

        /// <summary>
        /// Determines whether the specified image is potrait.
        /// </summary>
        /// <param name="image">The image.</param>
        /// <returns>
        /// 	<c>true</c> if the specified image is potrait; otherwise, <c>false</c>.
        /// </returns>
        public bool isPortrait(System.Drawing.Image image) {
            if (image.Height > image.Width) {
                return true;
            } else {
                return false;
            }
        }

        //public object watermark(string strFileName, string newStrFileName) {
        //    string FilePath = Server.MapPath("/images/");
        //    Bitmap bmp = System.Drawing.Image.FromFile(FilePath + strFileName + ".jpg");
        //    string strWatermark = "WATERMARK TEXT";
        //    Graphics canvas = Graphics.FromImage(resized);
        //    SizeF StringSizeF = default(SizeF);
        //    float DesiredWidth = 0;
        //    Font wmFont = default(Font);
        //    float RequiredFontSize = 0;
        //    float Ratio = 0;
        //    newStrFileName = FilePath + newStrFileName + ".jpg";
        //    wmFont = new Font("Verdana", 6, FontStyle.Bold);
        //    DesiredWidth = bmp.Width * 0.75;
        //    StringSizeF = canvas.MeasureString(strWatermark, wmFont);
        //    Ratio = StringSizeF.Width / wmFont.SizeInPoints;
        //    RequiredFontSize = DesiredWidth / Ratio;
        //    wmFont = new Font("Verdana", RequiredFontSize, FontStyle.Bold);
        //    canvas.DrawString(strWatermark, wmFont, new SolidBrush(Color.Beige), 0, 0);
        //    canvas.DrawString(strWatermark, wmFont, new SolidBrush(Color.FromArgb(128, 0, 0, 0)), 2, 2);
        //    canvas.DrawString(strWatermark, wmFont, new SolidBrush(Color.FromArgb(128, 255, 255, 255)), 0, 0);
        //    canvas.DrawString(strWatermark, wmFont, new SolidBrush(Color.FromArgb(128, 0, 0, 0)), 2, 2);
        //    canvas.DrawString(strWatermark, wmFont, new SolidBrush(Color.FromArgb(128, 255, 255, 255)), 0, 0);
        //    bmp.SetResolution(96, 96);
        //    bmp.Save(newStrFileName, ImageFormat.Jpeg);
        //    canvas.dispose();
        //    bmp.dispose();
        //}
    }
}
